package org.example;

import org.example.entity.CustomerInfo;
import org.example.entity.CustomerOrder;
import org.example.entity.CustomerRepayPlan;
import org.example.generator.DataGenerator;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 数据生成器使用示例
 */
public class DataGeneratorExample {
    
    public static void main(String[] args) {
        System.out.println("=== 数据生成器V2使用示例 ===");
        
        // 1. 生成完整数据集
        System.out.println("\n1. 生成完整数据集...");
        DataGenerator.DataGenerationResult result = DataGenerator.generateCompleteDataSet();
        
        List<CustomerInfo> customers = result.getCustomers();
        List<CustomerOrder> orders = result.getOrders();
        List<CustomerRepayPlan> repayPlans = result.getRepayPlans();
        
        System.out.println("生成完成！");
        System.out.println("客户数量: " + customers.size());
        System.out.println("订单数量: " + orders.size());
        System.out.println("还款计划数量: " + repayPlans.size());
        
        // 2. 计算Vintage分析
        System.out.println("\n2. 计算Vintage分析...");
        Map<String, DataGenerator.VintageAnalysis> vintageAnalysis = 
                DataGenerator.calculateVintageAnalysis(orders, repayPlans);
        
        System.out.println("Vintage分析结果:");
        for (DataGenerator.VintageAnalysis analysis : vintageAnalysis.values()) {
            System.out.println("  " + analysis.toString());
        }
        
        // 3. 统计关键指标
        System.out.println("\n3. 关键指标统计...");
        
        // 总放款金额
        BigDecimal totalLoanAmount = orders.stream()
                .map(CustomerOrder::getLoanAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("总放款金额: " + totalLoanAmount);
        
        // 在途余额
        BigDecimal outstandingAmount = orders.stream()
                .filter(order -> "未结清".equals(order.getStatus()))
                .map(CustomerOrder::getRemainPayAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("在途余额: " + outstandingAmount);
        
        // 浩瀚小贷在途余额
        BigDecimal haohanOutstanding = orders.stream()
                .filter(order -> "浩瀚小贷".equals(order.getProductName()) && "未结清".equals(order.getStatus()))
                .map(CustomerOrder::getRemainPayAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("浩瀚小贷在途余额: " + haohanOutstanding);
        
        // 逾期率
        long overdueCount = repayPlans.stream()
                .filter(plan -> "逾期".equals(plan.getRepayStatus()))
                .count();
        double overdueRate = (double) overdueCount / repayPlans.size() * 100;
        System.out.println("逾期率: " + String.format("%.2f%%", overdueRate));
        
        // 4. 产品分布统计
        System.out.println("\n4. 产品分布统计...");
        Map<String, Long> productDistribution = orders.stream()
                .collect(java.util.stream.Collectors.groupingBy(CustomerOrder::getProductName, java.util.stream.Collectors.counting()));
        
        for (Map.Entry<String, Long> entry : productDistribution.entrySet()) {
            System.out.println("  " + entry.getKey() + ": " + entry.getValue() + " 个订单");
        }
        
        // 5. 月度分布统计
        System.out.println("\n5. 月度分布统计...");
        Map<String, Long> monthlyDistribution = orders.stream()
                .collect(java.util.stream.Collectors.groupingBy(
                        order -> order.getFrTime().substring(0, 7),
                        java.util.stream.Collectors.counting()));
        
        monthlyDistribution.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .forEach(entry -> System.out.println("  " + entry.getKey() + ": " + entry.getValue() + " 个订单"));
        
        System.out.println("\n=== 示例完成 ===");
    }
}
